package com.example.field.util;

public class math {
    public static void count()
    {
        // 示例数据列
        double[] dataCol1 = {1, 2, 3, 4, 15};
        double[] dataCol2 = {2, 3, 4, 5, 20};

        // 计算相关系数
        double correlationCoefficient = calculateCorrelationCoefficient(dataCol1, dataCol2);

        // 将相关系数转化为百分比
        double correlationPercentage = Math.abs(correlationCoefficient) * 100;

        System.out.println("相关系数: " + correlationCoefficient);
        System.out.println("相关百分比: " + correlationPercentage);
    }

    //相关系数计算
    public static double calculateCorrelationCoefficient(double[] dataCol1, double[] dataCol2) {

        double sumX = 0;
        double sumY = 0;
        double sumXY = 0;
        double sumXX = 0;
        double sumYY = 0;

        int n = dataCol1.length;

        for (int i = 0; i < n; i++) {
            sumX += dataCol1[i];
            sumY += dataCol2[i];
            sumXY += dataCol1[i] * dataCol2[i];
            sumXX += dataCol1[i] * dataCol1[i];
            sumYY += dataCol2[i] * dataCol2[i];
        }

        double numerator = (n * sumXY) - (sumX * sumY);
        double denominator = Math.sqrt((n * sumXX - Math.pow(sumX, 2)) * (n * sumYY - Math.pow(sumY, 2)));

        // 计算相关系数

        return numerator / denominator;
    }
}
